#include <stdio.h>
#include <string.h>
// #include <stdlib.h>
#include <time.h>
#include "md5/md5.h"
#define INPUT_BUFFER_SIZE 100 * 1024
int main()
{
    const char *filename = "./readmed.md";
    FILE *file = fopen(filename, "rb");
    if (file)
    {
        printf("正在计算MD5值...\n");
        time_t startTime = time(NULL);
        md5_state_t md5StateT;
        md5_init(&md5StateT);
        char buffer[INPUT_BUFFER_SIZE];
        while (!feof(file))
        {
            size_t numberOfObjects = fread(buffer, sizeof(char), INPUT_BUFFER_SIZE, file);
            md5_append(&md5StateT, buffer, numberOfObjects);
        }
        md5_byte_t digest[16];
        md5_finish(&md5StateT, digest);
        char md5String[33] = { '\0' }, hexBuffer[3];
        for (size_t i = 0; i != 16; ++i)
        {
            if (digest[i] < 16)
                sprintf(hexBuffer, "0%x", digest[i]);
            else
                sprintf(hexBuffer, "%x", digest[i]);
            strcat(md5String, hexBuffer);
        }
        time_t endTime = time(NULL);
        fclose(file);
        printf("计算完毕：%s，耗时%u秒\n", md5String, endTime - startTime);
    }
    else
        printf("无法打开文件：%s\n", filename);
        
    system("md5sum *.md");
    return 0;
}
